package com.offer;


import com.bean.TreeNode;
import com.utils.TreeUtils;

public class Test55 {

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        } else {
            return Math.abs(getLevel(root.left) - getLevel(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
        }
    }

    private int getLevel(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            return Math.max(getLevel(root.left), getLevel(root.right)) + 1;
        }
    }

    public static void main(String[] args) {
        Test55 test = new Test55();

        TreeNode node = TreeUtils.buildTree("3,9,20,null,null,15,7");

        System.out.println(test.isBalanced(node));
    }

}
